//
//  FileTableViewCell.m
//  OAProject
//
//  Created by yhj on 2017/3/13.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "FileTableViewCell.h"

@interface FileTableViewCell ()

@property(nonatomic,strong)UIView *bgView;

// 图片
@property(nonatomic,strong)UIImageView *imageUrl;

// 名字
@property(nonatomic,strong)UILabel *nameLabel;

// 大小
@property(nonatomic,strong)UILabel *numberLabel;

// 下载
@property(nonatomic,strong)UIButton *downBtn;

@end

@implementation FileTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        // ➡️Cell类型
        self.accessoryType=UITableViewCellAccessoryNone;

        self.imageUrl.image=[UIImage imageNamed:@"pic_select"];

        self.imageUrl.hidden=NO;

        self.bgView.hidden=NO;

        self.nameLabel.font=Font_Number(16);

        self.numberLabel.font=Font_Number(14);

        self.downBtn.hidden=NO;

        self.backgroundColor=WhiteColor;
    }
    return self;
}

// imageUrl
-(UIImageView *)imageUrl
{
    if (!_imageUrl) {
        _imageUrl=[UIImageView new];
        _imageUrl.userInteractionEnabled=YES;
        [self.contentView addSubview:_imageUrl];
        [_imageUrl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(kmargin*2);
            make.size.mas_equalTo(CGSizeMake(kmargin*5,kmargin*4));
        }];

        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent)];
        [self.imageUrl addGestureRecognizer:tap];
    }
    return _imageUrl;
}

-(void)tapEvent
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showImageFullWithFileListModel:cell:)]) {
        [self.delegate showImageFullWithFileListModel:self.model cell:self];
    }
}


// nameLabel
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel=[self getLabelWithText:@"主机错误分析.docx" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.imageUrl.mas_right).offset(kmargin);
            make.top.mas_equalTo(self.imageUrl.mas_top);
            make.width.mas_equalTo(APPW/2);
        }];
    }
    return _nameLabel;
}

// numberLabel
- (UILabel *)numberLabel
{

    if (!_numberLabel) {
        _numberLabel=[self getLabelWithText:@"456.23k" font:Font_Number(14) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.contentView addSubview:_numberLabel];
        [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameLabel.mas_left);
            make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(kmargin/2);
        }];
    }
    return _numberLabel;
}

// downBtn
-(UIButton *)downBtn
{
    if (!_downBtn) {
        _downBtn=[AppUI createButtonWithSupeView:self.contentView andWithNormalImage:[UIImage imageNamed:@"fileDown"] andWithSelectImage:[UIImage imageNamed:@"fileDown"] andWithBtnAction:^{

    if (self.delegate&&[self.delegate respondsToSelector:@selector(downLoadFileWithFileListModel:cell:)])
            {
                [self.delegate downLoadFileWithFileListModel:self.model cell:self];
            }

        }];
        [self.contentView addSubview:_downBtn];
        [_downBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin*4);
            make.centerY.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(kmargin*3,kmargin*3));
        }];
    }
    return _downBtn;
}


-(void)setModel:(FileListModel *)model
{
    _model=model;

    [self.imageUrl sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"pic_select"]];
    
    self.nameLabel.text=model.name;

    self.numberLabel.text=[Util transformedValue:model.size];
}

@end
